/**
 * H5 环境
 */
// #ifdef H5
import Wx from "weixin-js-sdk";
import Sign from "./sign.js";

const wxPay = (option) => {
	console.log('发起H5支付：', option)
	let url = location.href.split('#')[0];
	return new Promise((resolve, reject) => {
		Sign.wxSign(url).then(res => {
			Wx.chooseWXPay({
				timestamp: option.timeStamp,
				nonceStr: option.nonceStr,
				package: option.package,
				signType: option.signType,
				paySign: option.paySign,
				success: res => resolve(res),
				cancel: res => reject(res)
			});
		}).catch(reject);
	});
}
// #endif

/**
 * 非 H5 环境
 */
// #ifndef H5
const wxPay = (option) => {
	console.log('发起小程序支付')
	const options = {
		timeStamp: '',
		nonceStr: '',
		package: '',
		signType: '',
		paySign: '',
		...option
	};
	return new Promise((resolve, reject) => {
		uni.requestPayment({
			provider: 'wxpay',
			timeStamp: options.timeStamp,
			nonceStr: options.nonceStr,
			package: options.package,
			signType: options.signType,
			paySign: options.paySign,
			success: res => resolve(res),
			fail: res => reject(res)
		});
	});
};

// #endif

/**
 * 导出系统支持的支付方式
 */
export default {
	wxPay
}
